<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="职位名称" prop="positionName">
              <el-select v-model="queryParams.positionName" placeholder="请选择职位名称" clearable >
                <el-option v-for="dict in app_position" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="薪资待遇" prop="salaryRange">
              <el-select v-model="queryParams.salaryRange" placeholder="请选择薪资待遇" clearable >
                <el-option v-for="dict in app_salary_range" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="工资结算方式" prop="payMethod">
              <el-select v-model="queryParams.payMethod" placeholder="请选择工资结算方式" clearable >
                <el-option v-for="dict in app_pay_method" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="经验要求" prop="experienceReq">
              <el-select v-model="queryParams.experienceReq" placeholder="请选择经验要求" clearable >
                <el-option v-for="dict in app_work_experience" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="工作地区编码" prop="workAreaCode">
              <el-input v-model="queryParams.workAreaCode" placeholder="请输入工作地区编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="所属企业ID" prop="enterpriseId">
              <el-input v-model="queryParams.enterpriseId" placeholder="请输入所属企业ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="发布时间" prop="publishTime">
              <el-date-picker clearable
                v-model="queryParams.publishTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择发布时间"
              />
            </el-form-item>
            <el-form-item label="招聘状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择招聘状态" clearable >
                <el-option v-for="dict in app_recruit_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:jobPosition:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['system:jobPosition:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['system:jobPosition:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:jobPosition:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="jobPositionList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="职位ID" align="center" prop="positionId" v-if="false" />
        <el-table-column label="职位标题" align="center" prop="positionTitle" />
        <el-table-column label="职位名称" align="center" prop="positionName">
          <template #default="scope">
            <dict-tag :options="app_position" :value="scope.row.positionName"/>
          </template>
        </el-table-column>
        <el-table-column label="招聘人数" align="center" prop="recruitCount">
          <template #default="scope">
            <dict-tag :options="app_recruit_count" :value="scope.row.recruitCount"/>
          </template>
        </el-table-column>
        <el-table-column label="薪资待遇" align="center" prop="salaryRange">
          <template #default="scope">
            <dict-tag :options="app_salary_range" :value="scope.row.salaryRange"/>
          </template>
        </el-table-column>
        <el-table-column label="薪资详细说明" align="center" prop="salaryDetail" />
        <el-table-column label="工资结算方式" align="center" prop="payMethod">
          <template #default="scope">
            <dict-tag :options="app_pay_method" :value="scope.row.payMethod"/>
          </template>
        </el-table-column>
        <el-table-column label="年龄要求" align="center" prop="ageRange">
          <template #default="scope">
            <dict-tag :options="app_age_range" :value="scope.row.ageRange"/>
          </template>
        </el-table-column>
        <el-table-column label="学历要求" align="center" prop="educationReq">
          <template #default="scope">
            <dict-tag :options="app_education_level" :value="scope.row.educationReq"/>
          </template>
        </el-table-column>
        <el-table-column label="经验要求" align="center" prop="experienceReq">
          <template #default="scope">
            <dict-tag :options="app_work_experience" :value="scope.row.experienceReq"/>
          </template>
        </el-table-column>
        <el-table-column label="岗位熟练程度" align="center" prop="skillLevel">
          <template #default="scope">
            <dict-tag :options="app_skill_level" :value="scope.row.skillLevel"/>
          </template>
        </el-table-column>
        <el-table-column label="资格证书要求" align="center" prop="certificateReq">
          <template #default="scope">
            <dict-tag :options="app_certificate_req" :value="scope.row.certificateReq"/>
          </template>
        </el-table-column>
        <el-table-column label="工期长度" align="center" prop="workDuration">
          <template #default="scope">
            <dict-tag :options="app_work_duration" :value="scope.row.workDuration"/>
          </template>
        </el-table-column>
        <el-table-column label="用工方式" align="center" prop="workType">
          <template #default="scope">
            <dict-tag :options="app_work_type" :value="scope.row.workType"/>
          </template>
        </el-table-column>
        <el-table-column label="工作地区编码" align="center" prop="workAreaCode" />
        <el-table-column label="所属企业ID" align="center" prop="enterpriseId" />
        <el-table-column label="发布人ID" align="center" prop="publisherId" />
        <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="招聘状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="app_recruit_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="浏览量" align="center" prop="viewCount" />
        <el-table-column label="联系次数" align="center" prop="contactCount" />
        <el-table-column label="收藏数" align="center" prop="favoriteCount" />
        <el-table-column label="分享次数" align="center" prop="shareCount" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:jobPosition:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:jobPosition:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改职位信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="jobPositionFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="职位标题" prop="positionTitle">
          <el-input v-model="form.positionTitle" placeholder="请输入职位标题" />
        </el-form-item>
        <el-form-item label="职位名称" prop="positionName">
          <el-select v-model="form.positionName" placeholder="请选择职位名称">
            <el-option
                v-for="dict in app_position"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="招聘人数" prop="recruitCount">
          <el-select v-model="form.recruitCount" placeholder="请选择招聘人数">
            <el-option
                v-for="dict in app_recruit_count"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="薪资待遇" prop="salaryRange">
          <el-select v-model="form.salaryRange" placeholder="请选择薪资待遇">
            <el-option
                v-for="dict in app_salary_range"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="薪资详细说明" prop="salaryDetail">
          <el-input v-model="form.salaryDetail" placeholder="请输入薪资详细说明" />
        </el-form-item>
        <el-form-item label="工资结算方式" prop="payMethod">
          <el-select v-model="form.payMethod" placeholder="请选择工资结算方式">
            <el-option
                v-for="dict in app_pay_method"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄要求" prop="ageRange">
          <el-select v-model="form.ageRange" placeholder="请选择年龄要求">
            <el-option
                v-for="dict in app_age_range"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学历要求" prop="educationReq">
          <el-select v-model="form.educationReq" placeholder="请选择学历要求">
            <el-option
                v-for="dict in app_education_level"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经验要求" prop="experienceReq">
          <el-select v-model="form.experienceReq" placeholder="请选择经验要求">
            <el-option
                v-for="dict in app_work_experience"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位熟练程度" prop="skillLevel">
          <el-select v-model="form.skillLevel" placeholder="请选择岗位熟练程度">
            <el-option
                v-for="dict in app_skill_level"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资格证书要求" prop="certificateReq">
          <el-select v-model="form.certificateReq" placeholder="请选择资格证书要求">
            <el-option
                v-for="dict in app_certificate_req"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工期长度" prop="workDuration">
          <el-select v-model="form.workDuration" placeholder="请选择工期长度">
            <el-option
                v-for="dict in app_work_duration"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用工方式" prop="workType">
          <el-select v-model="form.workType" placeholder="请选择用工方式">
            <el-option
                v-for="dict in app_work_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作地区编码" prop="workAreaCode">
          <el-input v-model="form.workAreaCode" placeholder="请输入工作地区编码" />
        </el-form-item>
        <el-form-item label="所属企业ID" prop="enterpriseId">
          <el-input v-model="form.enterpriseId" placeholder="请输入所属企业ID" />
        </el-form-item>
        <el-form-item label="发布人ID" prop="publisherId">
          <el-input v-model="form.publisherId" placeholder="请输入发布人ID" />
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime">
          <el-date-picker clearable
            v-model="form.publishTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="招聘状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择招聘状态">
            <el-option
                v-for="dict in app_recruit_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="JobPosition" lang="ts">
import { listJobPosition, getJobPosition, delJobPosition, addJobPosition, updateJobPosition } from '@/api/system/jobPosition';
import { JobPositionVO, JobPositionQuery, JobPositionForm } from '@/api/system/jobPosition/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { app_work_duration, app_salary_range, app_recruit_status, app_work_experience, app_certificate_req, app_skill_level, app_recruit_count, app_age_range, app_pay_method, app_work_type, app_position, app_education_level } = toRefs<any>(proxy?.useDict('app_work_duration', 'app_salary_range', 'app_recruit_status', 'app_work_experience', 'app_certificate_req', 'app_skill_level', 'app_recruit_count', 'app_age_range', 'app_pay_method', 'app_work_type', 'app_position', 'app_education_level'));

const jobPositionList = ref<JobPositionVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const jobPositionFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: JobPositionForm = {
  positionTitle: undefined,
  positionName: undefined,
  recruitCount: undefined,
  salaryRange: undefined,
  salaryDetail: undefined,
  payMethod: undefined,
  ageRange: undefined,
  educationReq: undefined,
  experienceReq: undefined,
  skillLevel: undefined,
  certificateReq: undefined,
  workDuration: undefined,
  workType: undefined,
  workAreaCode: undefined,
  enterpriseId: undefined,
  publisherId: undefined,
  publishTime: undefined,
  status: undefined,
}
const data = reactive<PageData<JobPositionForm, JobPositionQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    positionName: undefined,
    salaryRange: undefined,
    payMethod: undefined,
    experienceReq: undefined,
    workAreaCode: undefined,
    enterpriseId: undefined,
    publishTime: undefined,
    status: undefined,
    params: {
    }
  },
  rules: {
    positionName: [
      { required: true, message: "职位名称不能为空", trigger: "change" }
    ],
    recruitCount: [
      { required: true, message: "招聘人数不能为空", trigger: "change" }
    ],
    salaryRange: [
      { required: true, message: "薪资待遇不能为空", trigger: "change" }
    ],
    workAreaCode: [
      { required: true, message: "工作地区编码不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询职位信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listJobPosition(queryParams.value);
  jobPositionList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  jobPositionFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: JobPositionVO[]) => {
  ids.value = selection.map(item => item.positionId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加职位信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: JobPositionVO) => {
  reset();
  const _positionId = row?.positionId || ids.value[0]
  const res = await getJobPosition(_positionId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改职位信息";
}

/** 提交按钮 */
const submitForm = () => {
  jobPositionFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.positionId) {
        await updateJobPosition(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addJobPosition(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: JobPositionVO) => {
  const _positionIds = row?.positionId || ids.value;
  await proxy?.$modal.confirm('是否确认删除职位信息编号为"' + _positionIds + '"的数据项？').finally(() => loading.value = false);
  await delJobPosition(_positionIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('system/jobPosition/export', {
    ...queryParams.value
  }, `jobPosition_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
